package com.liugh.service.zhongche.Imp;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.liugh.entity.standard.OriginnalName;
import com.liugh.entity.standard.StandardItem;
import com.liugh.mapper.zhongche.OriginalNameMapper;
import com.liugh.mapper.zhongche.StandardItemMapper;
import com.liugh.service.zhongche.IESModelService;
import com.liugh.service.zhongche.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Fatao on 2018/9/27
 */
@Service
public class SearchServiceImpl extends ServiceImpl<StandardItemMapper,StandardItem> implements ISearchService {

    @Autowired
    private StandardItemMapper standardItemMapper;


    @Autowired
    private OriginalNameMapper originalNameMapper;

    @Autowired
    IESModelService modelService;

    /**
     * 标准的简单查询,根据no和name.
     * @param standNo .
     * @param standName .
     * @return .
     */
    @Override
    public Page<StandardItem> simpleSearch(String standNo, String standName,Page<StandardItem> page) {

        List<StandardItem> list;

        if (standNo != null && standName == null) {

            list = standardItemMapper.selectPage(page,new EntityWrapper<StandardItem>().like("stand_no", standNo));

        } else if (standNo == null && standName != null) {
            list = standardItemMapper.selectPage(page,new EntityWrapper<StandardItem>().like("stand_name",standName));
        } else {
            list = standardItemMapper.selectPage(page, new EntityWrapper<StandardItem>().like("stand_no", standNo)
                    .like("stand_name", standName));
        }

        page.setRecords(list);
        return page;

    }




    /**
     * 模糊查询,根据标准名称 .
     * @param key1 .
     * @param key2 .
     * @param key3 .
     * @param page .
     * @return
     */
    @Override
    public Page<StandardItem> complexSearchByName(String key1,String key2,String key3,Page<StandardItem> page) {
        List<StandardItem> list;
        if (key1 != null) {

            list = standardItemMapper.selectPage(page,new EntityWrapper<StandardItem>().like("stand_name", key1));

            if (key2 != null) {

                list = list.stream().filter(standardItem -> standardItem.getStandName().contains(key2)).collect(Collectors.toList());
            }
            if (key3 != null) {

                list = list.stream().filter(standardItem -> standardItem.getStandName().contains(key3)).collect(Collectors.toList());
            }

            page.setRecords(list);
            return page;
        } else {
            return null;
        }
    }

    /**
     * 保存原始文件名，远程文件名.
     * @param remoteName .
     * @param originName .
     */
    @Override
    public boolean addOriginName(String remoteName, String originName) {
        return originalNameMapper.insert(new OriginnalName(remoteName, originName)) > 0;
    }

    /**
     * 通过远程文件名获取原始文件名 .
     * @param remoteName .
     * @return .
     */
    @Override
    public String getOriginName(String remoteName) {
        OriginnalName originnalName = originalNameMapper.selectList(new EntityWrapper<OriginnalName>().where("remote_name = #{0}",remoteName)).get(0);
        return originnalName != null ? originnalName.getOriginName() : null;
    }

    @Override
    public Page fullTextQuery(Page<StandardItem> page, String key)  {
        try {
            page = modelService.fullTextQuery(page,key);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return page;
    }


}
